package UI;

import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import Logic.SQLReader;
import UI.MainFrame.LogOut;
import UI.BaseModels.BCBSPanel;
import UI.BaseModels.HeaderLabel;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import threads.BaseRunnableBCBS;

/**
 * The Class MainMenuAdmin.
 */
public class MainMenuAdmin extends BCBSPanel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6825289748794566322L;

	/** The btn add user. */
	private JButton btnAddUser;

	/** The btn edit user. */
	private JButton btnEditUser;

	/** The btn show users. */
	private JButton btnShowUsers;

	/** The btn log out. */
	private JButton btnLogOut;

	/** The lbl exchange value. */
	private JLabel lblExchangeValue;
	private JTextField textFieldExch;

	private JButton buttonsetRate;

	/**
	 * s Instantiates a Administrator panel.
	 */
	public MainMenuAdmin() {

		HeaderLabel header = new HeaderLabel("Administration Panel");
		this.add(header);

		btnAddUser = new JButton("Add User");
		btnAddUser.setBounds(82, 90, 140, 49);
		btnAddUser.addActionListener(new CreateNewUserPanel());
		add(btnAddUser);

		btnEditUser = new JButton("Edit User");
		btnEditUser.setBounds(82, 150, 140, 49);
		btnEditUser.addActionListener(new EditUserPanel());
		add(btnEditUser);

		btnShowUsers = new JButton("List Users");
		btnShowUsers.setBounds(82, 210, 140, 49);
		btnShowUsers.addActionListener(new ListUsersPanel());
		add(btnShowUsers);

		btnLogOut = new JButton("Log Out");
		btnLogOut.setBounds(99, 401, 106, 34);
		btnLogOut.addActionListener(new LogOut());
		add(btnLogOut);

		lblExchangeValue = new JLabel("");
		lblExchangeValue.setBounds(162, 65, 46, 14);
		add(lblExchangeValue);

		feedbackLabel.setLocation(0, 365);

		textFieldExch = new JTextField();
		textFieldExch.setHorizontalAlignment(SwingConstants.CENTER);
		textFieldExch.setBounds(99, 289, 106, 20);
		add(textFieldExch);
		textFieldExch.setColumns(10);

		JLabel lblNewLabel = new JLabel("Set Exchange rate:");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(82, 270, 140, 14);
		add(lblNewLabel);

		buttonsetRate = new JButton("Set Rate");
		buttonsetRate.setBounds(106, 320, 93, 34);
		buttonsetRate.addActionListener(new SetExchangeRate());
		add(buttonsetRate);
	}

	private class CreateNewUserPanel implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.create_User, null);
		}
	}

	private class EditUserPanel implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.edit_user, null);
		}
	}

	private class ListUsersPanel implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			UI.MainFrame.switchToPanel(UI.MainFrame.list_users, null);
		}
	}

	private class SetExchangeRate implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			setExchangeRate();
		}
	}

	/**
	 * See the run-method of this class for details.
	 * 
	 */
	private class GetExchangeRateRunnable extends BaseRunnableBCBS {

		/**
		 * Gets the Exchange Rate from the database and sets the Exchange Rate
		 * text field to the value fetched.
		 */
		public void run() {
			SQLReader reader = new SQLReader();
			feedback("Fetching Exchange rate...", true);
			buttonsetRate.setEnabled(false);
			try {
				textFieldExch.setText(String.valueOf(reader.getExchangeRate()));
			} catch (SQLException e) {
				feedback("Error getting Exchange rate", false);
			}
			buttonsetRate.setEnabled(true);
			feedback("", true);

		}
	}

	/**
	 * See the run-method of this class for details.
	 * 
	 */
	private class SetExchangeRateRunnable extends BaseRunnableBCBS {

		/**
		 * Sets the Exchange rate to the value specified by user interface.
		 */
		public void run() {
			SQLReader reader = new SQLReader();
			buttonsetRate.setEnabled(false);
			try {
				double rate = Double.valueOf(textFieldExch.getText().trim());
				reader.setExchangeRate(rate);
				feedback("Exchange rate set!", true);
			} catch (SQLException e) {
				feedback("Error getting Exchange rate", false);
			} catch (NumberFormatException e) {
				feedback("Please input a valid amount", false);
			}
			buttonsetRate.setEnabled(true);
		}
	}

	/**
	 * Creates a new Runnable of the type getExchangeRate, sets it to the
	 * current runnable for the panel, places it in its own thread and runs it.
	 */
	public void getExchangeRate() {
		runnable = new GetExchangeRateRunnable();
		Thread t = new Thread(runnable);
		t.start();
	}

	/**
	 * Creates a new Runnable of the type setExchangeRate, sets it to the
	 * current runnable for the panel, places it in its own thread and runs it.
	 */
	private void setExchangeRate() {
		runnable = new SetExchangeRateRunnable();
		Thread t = new Thread(runnable);
		t.start();
	}
}
